def rotate(nums, k):
    """
    原地旋转数组元素
    参数:
    nums -- 待旋转的数组
    k -- 向右旋转的步数
    """
    n = len(nums)
    if n == 0:
        return
    # 取模处理k大于数组长度的情况
    k = k % n
    # 当无需旋转时直接返回
    if k == 0:
        return
    # 计算需要进行的环状循环次数
    count = gcd(k, n)
    # 执行环状替换
    for start in range(count):
        current = start
        prev = nums[start]
        while True:
            next_idx = (current + k) % n
            # 交换当前元素和下一个位置的元素
            nums[next_idx], prev = prev, nums[next_idx]
            current = next_idx

            # 如果回到起点则结束内层循环
            if current == start:
                break


def gcd(x, y):
    """计算两个数的最大公约数"""
    if y == 0:
        return x
    return gcd(y, x % y)


# 测试用例
if __name__ == "__main__":
    test_cases = [
        ([1, 2, 3, 4, 5, 6, 7], 3, [5, 6, 7, 1, 2, 3, 4]),
        ([1, 2, 3, 4, 5, 6], 2, [5, 6, 1, 2, 3, 4]),
        ([1, 2, 3, 4], 2, [3, 4, 1, 2]),
        ([1], 5, [1]),
        ([], 3, [])
    ]

    passed = 0
    for nums, k, expected in test_cases:
        # 创建副本进行旋转操作
        nums_copy = nums.copy()
        rotate(nums_copy, k)
        result = nums_copy == expected

        if result:
            passed += 1
        else:
            print(f"测试失败! 输入: {nums}, k={k}")
            print(f"期望: {expected}")
            print(f"实际: {nums_copy}")

    print(f"测试结果: {passed}/{len(test_cases)} 通过")
